<?php

class Employer
{
	function delete_company($companyID)
	{
		$conn = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) or die('An error has occured: Could not connect to the MySQL database.');
		mysql_select_db(DB_NAME) or die("An error has occured:  Could not connect to the table.");
		
		$sql = "delete from wms_employer where employer_id = '%s'";
		$sql = sprintf($sql
			,mysql_real_escape_string($companyID));
			
		$result = mysql_query($sql);
		if(!$result) die("An error has occured: " . mysql_error());
		
		mysql_close($conn);
		
		return $result;
	}
	
	function update_company($companyID, $name, $industry, $address, $city, $state, $zip, $address2, $city2, $state2, $zip2, $phone, $fax, $website, $email)
	{
		$conn = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) or die('An error has occured: Could not connect to the MySQL database.');
		mysql_select_db(DB_NAME) or die("An error has occured:  Could not connect to the table.");
		
		$sql = "update wms_employer ";
		$sql .= "set company_name = '%s', industry = '%s', address = '%s', city = '%s', state = '%s', zip = '%s', second_address = '%s', second_city = '%s', second_state = '%s', second_zip = '%s', phone_number = '%s', fax_number = '%s', website = '%s', email_address = '%s' where employer_id = '%s'";
		$sql = sprintf($sql
			,mysql_real_escape_string($name)
			,mysql_real_escape_string($industry)
			,mysql_real_escape_string($address)
			,mysql_real_escape_string($city)
			,mysql_real_escape_string($state)
			,mysql_real_escape_string($zip)
			,mysql_real_escape_string($address2)
			,mysql_real_escape_string($city2)
			,mysql_real_escape_string($state2)
			,mysql_real_escape_string($zip2)
			,mysql_real_escape_string($phone)
			,mysql_real_escape_string($fax)
			,mysql_real_escape_string($website)
			,mysql_real_escape_string($email)
			,mysql_real_escape_string($companyID));
		
		$result = mysql_query($sql);
		if(!$result) die("An error has occured: " . mysql_error());
		
		mysql_close($conn);
		
		return $result;
	}
	
	function get_company_info($companyID)
	{
		$conn = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) or die('An error has occured: Could not connect to the MySQL database.');
		mysql_select_db(DB_NAME) or die("An error has occured:  Could not connect to the table.");
		
		$sql = "select * from wms_employer where employer_id = '%s';";
		$sql = sprintf($sql
			,mysql_real_escape_string($companyID));
			
		$result = mysql_query($sql);
		if(!$result) die("An error has occured: " . mysql_error());
		
		mysql_close($conn);
		
		$row = mysql_fetch_assoc($result);
		
		$companyInfo = array(
			'id'		=> $row['employer_id'],
			'name' 		=> $row['company_name'],
			'industry'	=> $row['Industry'],
			'address' 	=> $row['address'],
			'city'		=> $row['city'],
			'state'		=> $row['state'],
			'zip'		=> $row['zip'],
			'address2'	=> $row['second_address'],
			'city2'		=> $row['second_city'],
			'state2'	=> $row['second_state'],
			'zip2'		=> $row['second_zip'],
			'phone'		=> $row['phone_number'],
			'fax'		=> $row['fax_number'],
			'website'	=> $row['website'],
			'email'		=> $row['email_address']);
		
		return $companyInfo;
	}
	
	function get_companies()
	{
		$conn = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) or die('An error has occured: Could not connect to the MySQL database.');
		mysql_select_db(DB_NAME) or die("An error has occured:  Could not connect to the table.");
		
		$sql = "select employer_id, company_name from wms_employer order by company_name;";	
			
		$result = mysql_query($sql);
		if(!$result) die("An error has occured: " . mysql_error());
		
		mysql_close($conn);	
		
		while($row = mysql_fetch_assoc($result))
		{
			$id = $row['employer_id'];
			$name = $row['company_name'];
			
			$companies[$id] = $name;
		}
		
		if(!isset($companies)) $companies[0] = "";
		
		return $companies;
	}
	
	function add_company($compName, $industry, $address, $city, $state, $zipcode, $address2, $city2, $state2, $zipcode2, $phone, $fax, $website, $email)
	{
		$conn = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) or die('An error has occured: Could not connect to the MySQL database.');
		mysql_select_db(DB_NAME) or die("An error has occured:  Could not connect to the table.");
			
		$sql = "insert into wms_employer ";
		$sql .= "(company_name, industry, address, city, state, zip, second_address, second_city, second_state, second_zip, phone_number, fax_number, website, email_address, date_register) ";
		$sql .= "values('%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s', '%s');";
			
		$sql = sprintf($sql 
			,mysql_real_escape_string($compName)
			,mysql_real_escape_string($industry)
			,mysql_real_escape_string($address)
			,mysql_real_escape_string($city)
			,mysql_real_escape_string($state)
			,mysql_real_escape_string($zipcode)
			,mysql_real_escape_string($address2)
			,mysql_real_escape_string($city2)
			,mysql_real_escape_string($state2)
			,mysql_real_escape_string($zipcode2)
			,mysql_real_escape_string($phone)
			,mysql_real_escape_string($fax)
			,mysql_real_escape_string($website)
			,mysql_real_escape_string($email)
			,mysql_real_escape_string(date('Y-m-d H:i:s')));
			
		$result = mysql_query($sql);
		
		if(!$result) die("An error has occured: " . mysql_error());
		mysql_close($conn);	
	}
	
	function add_contact($fk_employer_id, $fname, $lname, $phone_number, $fax_number, $email_address)
	{
		$conn = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) or die('An error has occured: Could not connect to the MySQL database.');
		mysql_select_db(DB_NAME) or die("An error has occured:  Could not connect to the table.");
		
		$sql = 'insert into wms_empcontact ';
		$sql .= '(fk_employer_id, fname, lname, phone_number, fax_number, email_address) ';
		$sql .= "values('%s', '%s', '%s', '%s', '%s', '%s');";
		
		$sql = sprintf($sql
			,mysql_real_escape_string($fk_employer_id)
			,mysql_real_escape_string($fname)
			,mysql_real_escape_string($lname)
			,mysql_real_escape_string($phone_number)
			,mysql_real_escape_string($fax_number)
			,mysql_real_escape_string($email_address));
		
		$result = mysql_query($sql);
		if(!$result) die("An error has occured: " . mysql_error());
		
		mysql_close($conn);
		
		return $result;
	}
	
	function listEmployers()
	{
		$conn = mysql_connect(DB_HOSTNAME, DB_USERNAME, DB_PASSWORD) or die('An error has occured: Could not connect to the MySQL database.');
		mysql_select_db(DB_NAME) or die("An error has occured:  Could not connect to the table.");
		
		$sql = 'select * from wms_employer;';
		
		$result = mysql_query($sql);
		if(!$result) die("An error has occured: " . mysql_error());
		
		$row = mysql_fetch_assoc($result);
		
		$employerList = array();
		
		while($row = mysql_fetch_assoc($result))
		{
			$id = 0;
			
			$employer = array(
			'id'		=> $row['employer_id'],
			'name' 		=> $row['company_name'],
			'industry'	=> $row['Industry'],
			'address' 	=> $row['address'],
			'city'		=> $row['city'],
			'state'		=> $row['state'],
			'zip'		=> $row['zip'],
			'address2'	=> $row['second_address'],
			'city2'		=> $row['second_city'],
			'state2'	=> $row['second_state'],
			'zip2'		=> $row['second_zip'],
			'phone'		=> $row['phone_number'],
			'fax'		=> $row['fax_number'],
			'website'	=> $row['website'],
			'email'		=> $row['email_address'],
			'register'	=> $row['date_register'],
			'active' 	=> $row['is_active']);
			
			
			$employerList[$id++] = $employer;
		}
		
		return $employerList;
	}
}

?>